#include <Game/Include/GameLogic.h>

#include <windows.h>

#include "resource.h"

#include <BlockBreaker/WinApp.h>
#include <BlockBreaker/WinWindow.h>

#include <vld.h>

GameLogic* game_logic;
bool it_is_server;
std::string ip_server;

// -------------------------------------------------------------------------
LRESULT CALLBACK setup_dialog_handler(HWND hDlg,UINT wm,WPARAM wp,LPARAM lp)
{
	static HWND hlist;
	switch(wm)
	{
	case WM_INITDIALOG:
		{
			return true;
		}
		break;
	case WM_COMMAND:
		switch(wp)
		{
			case IDOK:
			{				
				int checked;
				// ventana
				checked = SendDlgItemMessage(hDlg,ID_OPTION_PLAYER1,BM_GETCHECK,0,0);
				if( checked == BST_CHECKED )
				{
					it_is_server = true;
				}

				checked = SendDlgItemMessage(hDlg,ID_OPTION_PLAYER2,BM_GETCHECK,0,0);
				if( checked == BST_CHECKED )
				{
					it_is_server = false;
					BOOL success;
                    DWORD address;  
                    LRESULT lResult = SendDlgItemMessage ( hDlg, IDC_IPADDRESS1, IPM_GETADDRESS, 0, (LPARAM)&address);
                    char auxi[16];
                    sprintf(auxi, "%d.%d.%d.%d", FIRST_IPADDRESS(address), SECOND_IPADDRESS(address), THIRD_IPADDRESS(address), FOURTH_IPADDRESS(address));
                    ip_server = auxi;
				}

				EndDialog(hDlg,true);
				return 1;
			}
			break;
			case IDCANCEL:
			{
				EndDialog(hDlg,false);
				MessageBox(hDlg,L"Oh! You have chosen to quit this game!",L"Quit From Game",MB_ICONEXCLAMATION);
				return 1;
			}
			break;
			case ID_OPTION_PLAYER1:
				{
					HWND ip_address = GetDlgItem(hDlg, IDC_IPADDRESS1);
					EnableWindow(ip_address, FALSE);
				}
				break;
			case ID_OPTION_PLAYER2:
				{
					HWND ip_address = GetDlgItem(hDlg, IDC_IPADDRESS1);
					EnableWindow(ip_address, TRUE);
				}
				break;
		}		
		break;
	}
	
	return 0;
};


//---------------------------------------------------------------------------
LRESULT CALLBACK MainWndProc(HWND hWnd, UINT Msg,
                             WPARAM wParam, LPARAM lParam);
//---------------------------------------------------------------------------
INT WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInst,
				   LPSTR lpCmdLine, int nCmdShow)
{
    DLGPROC dlgproc;
    dlgproc=(DLGPROC)setup_dialog_handler;
    bool goon = (bool)DialogBox(hInstance,MAKEINTRESOURCE(IDD_DIALOG1),NULL,dlgproc);

    if( !goon)
    {
        return 0;
    }

    MSG   Msg;
	LPCTSTR ClsName = L"Win32OOP";
	LPCTSTR WndName = L"Block Breaker";

	// Initialize the application class
	WinApp* winApp = new WinApp(hInstance, ClsName, MainWndProc);
	winApp->Register();

	// Create the main window
	int win_width = 500;
	int win_height = 500;
	WinWindow* wnd = new WinWindow();
	wnd->Create(hInstance, ClsName, WndName, NULL, /*WS_EX_TOPMOST | WS_POPUP*/ WS_OVERLAPPEDWINDOW , 0, 0, 0, win_width, win_height);
	
    // Display the main window
	wnd->Show();
		
    game_logic = new GameLogic(wnd);
    bool successful_connection = true;
    if (it_is_server)
    {
        if(!game_logic->init_comm())
        {
            successful_connection = false;
            MessageBox(wnd->getHWND(),L"Oh! Player 2 has not connected after a while. Application will close, try again.",L"Player 2 has not connected",MB_ICONEXCLAMATION);
        }
    }
    else
    {
        if(!game_logic->init_comm(ip_server))
        {
            successful_connection = false;
            MessageBox(wnd->getHWND(),L"Oh! Cannot Connect with Player 1! Check IP Address and/or Ethernet connection. Application will close, try again.",L"Cannot Connect with Player 1",MB_ICONEXCLAMATION);
        }
    }
    
    if (successful_connection)
    {
        game_logic->load();
        game_logic->run();
	    // Process the main window's messages
	    while( GetMessage(&Msg, NULL, 0, 0) )
	    {
		    TranslateMessage(&Msg);
		    DispatchMessage(&Msg);
	    }

        game_logic->unload();
        game_logic->finish_comm();
    }
    delete game_logic;

	delete winApp;
	delete wnd;

	return 0;
}
//---------------------------------------------------------------------------
LRESULT CALLBACK MainWndProc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam)
{
	switch(Msg)
	{
    case WM_CLOSE:
	case WM_DESTROY:
        if (game_logic->hasTheGameFinished())
        {
            PostQuitMessage(WM_QUIT);
        }
        else
        {
            MessageBox(hWnd,L"Oh! You have to wait till game is finished!",L"Cannot Quit From Game",MB_ICONEXCLAMATION);
        }
		return 0;
        break;
    case WM_KEYDOWN:
        {
            switch (wParam)
            {
            case VK_RIGHT:
                game_logic->input(GameLogic::GameInputType::RIGHT_ARROW_PRESSED);
                break;
            case VK_LEFT:
                game_logic->input(GameLogic::GameInputType::LEFT_ARROW_PRESSED);
                break;
            case VK_SPACE:
                game_logic->changeView();
                break;
            }            
        }
        break;
    case WM_KEYUP:
        {
            switch (wParam)
            {
            case VK_RIGHT:
                game_logic->input(GameLogic::GameInputType::RIGHT_ARROW_RELEASED);
                break;
            case VK_LEFT:
                game_logic->input(GameLogic::GameInputType::LEFT_ARROW_RELEASED);
                break;            
            }
        }
        break;
	}

	return DefWindowProc(hWnd, Msg, wParam, lParam);
}
//-----------------------------------------------




